/*
 *  GDT.c
 *  xD-OS
 *
 *  Created by James Johns on 22/07/2009.
 *  Copyright 2009 xD-Dunce. All rights reserved.
 *
 */

#include "GDT.h"


void initGDT(){
	
	insertGDTEntry(0, 0, 0, 0);					// null descriptor
	insertGDTEntry(1, 0, 0xFFFFFFFF, 0x9AC0);
	insertGDTEntry(2, 0, 0xFFFFFFFF, 0x92C0);
	insertGDTEntry(3, 0, 0xFFFFFFFF, 0xFAC0);
	insertGDTEntry(4, 0, 0xFFFFFFFF, 0xF2C0);
	
	gdtDescriptor.size = (sizeof(struct gdt_descriptor) *5) -1;
	gdtDescriptor.address = (dWord)&gdt;
	
	loadGDT((dWord)&gdtDescriptor);
	
}


void insertGDTEntry(dWord entryNum, dWord base, dWord limit, Word flags){
	
	gdt[entryNum].limitLow	=		(limit & 0x0000FFFF);
	gdt[entryNum].limitMid =		(flags &0x00FF | ((limit & 0x000F0000) >> 16));
	
	gdt[entryNum].baseLow	=		(base & 0x0000FFFF);
	gdt[entryNum].baseMid	=		(base & 0x00FF0000)	 >> 16;
	gdt[entryNum].baseHigh	=		(base & 0xFF000000)  >> 24;
	
	gdt[entryNum].flags		=		((flags & 0xFF00) >> 8);
}



